package com.yjd.bi.etl.first;

import com.yjd.bi.first.model.FirstRunRecordModel;
import com.yjd.bi.first.service.IAppPayFirstService;
import com.yjd.bi.first.service.IFirstRunRecordService;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.job.BaseJob;

import java.util.Date;

/**
 * 首充记录job
 *
 * @author yjd-wap
 */
public class AppPayFirstJob extends BaseJob {

    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private IAppPayFirstService appPayFirstService;


    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private IFirstRunRecordService firstRunRecordService;

    // 用户对账单起始日期
    private final static int minDate = 20131221;

    @SuppressWarnings("unchecked")
    @Override
    protected boolean doExecute() throws Exception {


        Date stDate = DateUtil.addDay(DateUtil.timestampToDate(DateUtil
                .dateStringToTimestamp(DateUtil.timestampToDateString(
                        FrameUtil.getTime(), DateUtil.PATTERN_yyyyMMdd),
                        DateUtil.PATTERN_yyyyMMdd)), -1);
        int currtDate = Integer.parseInt(DateUtil.format(stDate,
                DateUtil.PATTERN_yyyyMMdd));
        FirstRunRecordModel jobRecordModel = firstRunRecordService.getModelOne(
                FrameUtil.newHashMap("type", 2, "sort", "  rund_date desc "), DbWREnums.WRITE);

        int starttime = minDate;
        if (jobRecordModel != null) {
                if(jobRecordModel.getRund_date()==currtDate){
                    logger.info("首充值最新数据已经生成,日期："+currtDate);
                    return true;
                }
                if (jobRecordModel.getRund_date() < currtDate) {
                    starttime = Integer.parseInt(DateUtil.format(DateUtil.addDay(DateUtil.parseDate(jobRecordModel.getRund_date() + "", DateUtil.PATTERN_yyyyMMdd), 1), DateUtil.PATTERN_yyyyMMdd));
                } else {
                    starttime = currtDate;
                }
        }

        do {
            Long startTimes = DateUtil.dateStringToTimestamp(starttime + "", DateUtil.PATTERN_yyyyMMdd);
            Long endTimes = startTimes + 86399;

            //生成当天首充时间
            appPayFirstService.generateAppPayFirst(startTimes, endTimes);
            FirstRunRecordModel firstModel = new FirstRunRecordModel();
            firstModel.setRund_date(starttime);
            firstModel.setStatus(1);
            firstModel.setType(2);
            Long currtime = FrameUtil.getTime(null);
            firstModel.setCreate_time(currtime);
            firstModel.setUpdate_time(currtime);
            firstRunRecordService.insert(firstModel);

            //开始时间加一天
            starttime = Integer.parseInt(DateUtil.format(DateUtil.addDay(DateUtil.parseDate(starttime + "", DateUtil.PATTERN_yyyyMMdd), 1), DateUtil.PATTERN_yyyyMMdd));
          logger.info("首次充值数据已生成，时间段："+startTimes+"--"+endTimes);
        } while (starttime <= currtDate);

        return true;
    }


    @Override
    protected String getJobType() {
        return AppPayFirstJob.class.getName();
    }

}
